package top.codestudy.servicetrain;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class SencondActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "SencondActivity";
    private Button mBtnStartService2;
    private Button mBtnStopService2;
    private Button mBtnBindService2;
    private Button mBtnUnbindService2;

    ServiceConnection serviceConnection2 = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MusicService.MyBinder service1 = (MusicService.MyBinder) service;
            Log.i(TAG, "onServiceConnected2: ComponentName ==> " + name.toString());
            Log.i(TAG, "onServiceConnected2: IBinder => " + service + ";MyBinder中获取service实例 = " + service1.getServiceName() );
            Log.d(TAG, "onServiceConnected2: name => " + ((MusicService.MyBinder) service).name);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG, "onServiceDisconnected2: ");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sencond);

        mBtnStartService2 = findViewById(R.id.btn_start_service2);
        mBtnStopService2 = findViewById(R.id.btn_stop_service2);
        mBtnBindService2 = findViewById(R.id.btn_bind_service2);
        mBtnUnbindService2 = findViewById(R.id.btn_unbind_service2);

        mBtnStartService2.setOnClickListener(this);
        mBtnStopService2.setOnClickListener(this);
        mBtnBindService2.setOnClickListener(this);
        mBtnUnbindService2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_start_service2:
                startMyService();
                break;
            case R.id.btn_stop_service2:
                stopMyService();
                break;
            case R.id.btn_bind_service2:
                bindMyService();
                break;
            case R.id.btn_unbind_service2:
                unbindMyService();
                break;

        }
    }

    private void startMyService() {
        Intent intent = new Intent(this,MusicService.class);
        startService(intent);
    }

    private void stopMyService() {
        Intent intent = new Intent(this,MusicService.class);
        stopService(intent);
    }

    private void bindMyService() {
        Intent intent = new Intent(this,MusicService.class);
        bindService(intent,serviceConnection2, Context.BIND_AUTO_CREATE);
    }

    private void unbindMyService() {
        unbindService(serviceConnection2);
    }
}